// pages/addTr/addTr.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoice_category_name: '',
    spec: '',
    unit: '',
    num: '',
    price: '',
    total_price: '',
    invoice_category: [],
    invoice_category_arr: [0,0],
    invoice_unit: [],
    source: '',
    index: '',
    showDelBtn: false
  },
  submit() {
    if (!this.data.invoice_category_name) {
      return wx.$Toast("请选择货物或应税劳动/劳务");
    }

    if (!this.data.total_price) {
      return wx.$Toast("请输入含税销售额");
    }
    // if(this.data.total_price!=this.data.price*this.data.num){
    //   return wx.$Toast("请修改含税单价和货物数量或含税销售额")
    // }
    let detail = {
      invoice_category_name: this.data.invoice_category_name,
      spec: this.data.spec,
      unit: this.data.unit,
      num: this.data.num,
      price: this.data.price,
      total_price: this.data.total_price
    };
    let arr = [];
    arr.push(detail)
    this.toPrevPage(arr);
    // wx.navigateBack()
  },
  toPrevPage(invoice_ext) {
    console.log(invoice_ext)

    let pages = getCurrentPages();
    console.log(pages)
    let that = pages[pages.length - 2];
    console.log(that);
    that.changeExt(invoice_ext)

    wx.navigateBack({
      complete: (res) => {},
    })
  },
  changeVal(e) {
    let {
      type
    } = e.currentTarget.dataset;
    let params = {}
    params[type] = e.detail.value;
    this.setData(params)
  },
  changePrice(e) {
    let {
      num,
      price
    } = this.data;
    if (num && price) {
      this.setData({
        total_price: num * price
      })
    }
  },
  eventValChange() {
    let {
      num,
      price
    } = this.data;
    if (num || price) {
      wx.$Toast('请修改或删除含税单价和货物数量', 5000);
    }
  },
  getInvoiceInfo() {
    wx.showLoading()
    wx.$api.naturalInvoiceInfo().then(res => {
      wx.hideLoading();
      console.log(res.data)
      let {
        invoice_category,
        invoice_unit
      } = res.data.data;
      if (res.data.code == 200) {
        let arr = [
          [...invoice_category],
          [...invoice_category[0].children]
        ];
        console.log(arr)
        this.setData({
          invoice_unit: invoice_unit,
          invoice_category: arr
        })
      }
    }).catch(ret => {
      wx.$Toast(ret.msg || "服务繁忙")
    })
  },
  bindColumnChange(e) {
    console.log(e);
    let {
      column,
      value
    } = e.detail;
    var data = {
      multiArray: this.data.invoice_category,
      multiIndex: this.data.invoice_category_arr
    };
    data.multiIndex[column] = value;
    switch (column) {
      case 0:
        data.multiArray[1] = this.data.invoice_category[0][value].children;
        break;
    }
    this.setData({
      invoice_category:data.multiArray,
      invoice_category_arr:data.multiIndex
    })
  },
  changeCol(e) {
    let {
      value
    } = e.detail, {
      type
    } = e.currentTarget.dataset, {
      invoice_category,
      invoice_unit
    } = this.data;
    if (type == 0) {
      console.log(invoice_category[0][value[0]].invoice_category_name + '*' + invoice_category[1][value[1]].invoice_category_name)
      this.setData({
        invoice_category_arr: value,
        invoice_category_name: invoice_category[0][value[0]].invoice_category_name + '*' + invoice_category[1][value[1]].invoice_category_name
      })
    } else if (type == 1) {
      this.setData({
        unit: invoice_unit[value]
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, options.col);
    if (options.col && options.index) {
      wx.$setTop('编辑条目')
      let col = JSON.parse(options.col);
      let index = JSON.parse(options.index)
      console.log(col)
      this.setData({
        invoice_category_name: col.invoice_category_name,
        spec: col.spec,
        unit: col.unit,
        num: col.num,
        price: col.price,
        total_price: col.total_price,
        index: index,
        showDelBtn: true
      })
    } else {
      wx.$setTop('新增条目')
    }
  },
  del() {
    let that = this;
    Dialog.confirm({
      title: '确认删除',
      message: '确定删除该条目？',
      asyncClose: true,
      className: 'btnWidth'
    }).then(() => {
      this.toPrevPage([]);
    }).catch(() => {
      Dialog.close();
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInvoiceInfo();
    let pages = getCurrentPages();
    console.log(pages)
    let that = pages[pages.length - 2];
    that.setData({
      isLoadData: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})